/*
 * Copyright 2008 the original author Ang Gao.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * BlogServerDialogBox.java 2008-12-31
 */
package ie.ucc.cs.anggao.finalproject.client.support.widget;

import ie.ucc.cs.anggao.finalproject.client.ui.BasePane;
import ie.ucc.cs.anggao.finalproject.client.ui.event.PostEvent;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author GaoAng
 * 
 */
public class BlogServerDialogBox {

	private BasePane source;
	
	public BlogServerDialogBox(BasePane source) {
		this.source = source;
	}

	private DecoratorPanel Wordpress(final DialogBox dialogbox) {

		final TextBox username = new TextBox();
		username.setSize("100", "20");
		final PasswordTextBox password = new PasswordTextBox();
		password.setSize("100", "20");
		final TextBox blogaddress = new TextBox();
		blogaddress.setSize("200", "20");

		HorizontalPanel usernamePanel = new HorizontalPanel();
		usernamePanel.add(new Label("User Name"));

		HorizontalPanel passwordPanel = new HorizontalPanel();
		passwordPanel.add(username);
		passwordPanel.add(new Label("PassWord: "));
		passwordPanel.add(password);
		passwordPanel.setSpacing(10);

		
		HorizontalPanel egPanel = new HorizontalPanel();
		egPanel.add(new Label("eg:http://anggao.wordpress.com/"));
		
		HorizontalPanel radioPanel = new HorizontalPanel();
		final RadioButton btnyes = new RadioButton("draft", "Yes");
		RadioButton btnno = new RadioButton("draft", "No");
		radioPanel.add(btnyes);
		radioPanel.add(btnno);
		
		
		HorizontalPanel btnPanel = new HorizontalPanel();
		Button publishButton = new Button("Publish", new ClickListener() {
			public void onClick(Widget sender) {
				source.fireEvent(new PostEvent(source, username.getText(), password.getText(), blogaddress.getText(), Boolean.valueOf(btnyes.isChecked()).toString(), "Wordpress"));
			}
		});

		Button closeButton = new Button("Close", new ClickListener() {
			public void onClick(Widget sender) {
				dialogbox.hide();
			}
		});
		
		
		btnPanel.add(publishButton);
		btnPanel.add(closeButton);
		btnPanel.setSpacing(10);


		Grid grid = new Grid(5, 2);

		grid.setWidget(0, 0, new Label("Blog Address:   "));
		grid.setWidget(0, 1, blogaddress);
		grid.setWidget(1, 0, egPanel);
		grid.setWidget(2, 0, usernamePanel);
		grid.setWidget(2, 1, passwordPanel);
		grid.setWidget(3, 0, new Label("Save as draft"));
		grid.setWidget(3, 1, radioPanel);
		grid.setWidget(4, 1, btnPanel);
		grid.getCellFormatter().setHorizontalAlignment(0, 1,
				HasHorizontalAlignment.ALIGN_LEFT);
		grid.getCellFormatter().setHorizontalAlignment(3, 1,
				HasHorizontalAlignment.ALIGN_RIGHT);

		// Wrap the content in a DecoratorPanel
		DecoratorPanel decPanel = new DecoratorPanel();
		decPanel.setWidget(grid);
		return decPanel;
	}

	private DecoratorPanel Blogger(final DialogBox dialogbox) {
		final TextBox username = new TextBox();
		username.setSize("100", "20");
		final PasswordTextBox password = new PasswordTextBox();
		password.setSize("100", "20");
		final TextBox blogaddress = new TextBox();
		blogaddress.setSize("200", "20");

		HorizontalPanel usernamePanel = new HorizontalPanel();
		usernamePanel.add(new Label("User Name"));

		HorizontalPanel passwordPanel = new HorizontalPanel();
		passwordPanel.add(username);
		passwordPanel.add(new Label("PassWord: "));
		passwordPanel.add(password);
		passwordPanel.setSpacing(10);

		
		
		HorizontalPanel radioPanel = new HorizontalPanel();
		final RadioButton btnyes = new RadioButton("draft", "Yes");
		RadioButton btnno = new RadioButton("draft", "No");
		radioPanel.add(btnyes);
		radioPanel.add(btnno);
		
		
		HorizontalPanel btnPanel = new HorizontalPanel();
		Button publishButton = new Button("Publish", new ClickListener() {
			public void onClick(Widget sender) {
				source.fireEvent(new PostEvent(source, username.getText(), password.getText(), blogaddress.getText(), Boolean.valueOf(btnyes.isChecked()).toString(), "Blogger"));
			}
		});

		Button closeButton = new Button("Close", new ClickListener() {
			public void onClick(Widget sender) {
				dialogbox.hide();
			}
		});
		
		
		btnPanel.add(publishButton);
		btnPanel.add(closeButton);
		btnPanel.setSpacing(10);


		Grid grid = new Grid(4, 2);

		grid.setWidget(0, 0, usernamePanel);
		grid.setWidget(0, 1, passwordPanel);
		grid.setWidget(1, 0, new Label("Save as draft"));
		grid.setWidget(1, 1, radioPanel);
		grid.setWidget(2, 1, btnPanel);
		grid.getCellFormatter().setHorizontalAlignment(0, 1,
				HasHorizontalAlignment.ALIGN_LEFT);
		grid.getCellFormatter().setHorizontalAlignment(3, 1,
				HasHorizontalAlignment.ALIGN_RIGHT);

		// Wrap the content in a DecoratorPanel
		DecoratorPanel decPanel = new DecoratorPanel();
		decPanel.setWidget(grid);
		return decPanel;
	}

	public DialogBox createDialog() {

		final DialogBox dialogbox = new DialogBox();

		DecoratedTabPanel tabPanel = new DecoratedTabPanel();
		tabPanel.setWidth("450px");
		tabPanel.setAnimationEnabled(true);

		tabPanel.add(this.Wordpress(dialogbox), "WordPress");
		tabPanel.add(this.Blogger(dialogbox), "Blogger");
		tabPanel.selectTab(0);

		dialogbox.add(tabPanel);

		return dialogbox;
	}
}
